﻿using System;
using System.ComponentModel;


namespace Marmat.Forms
{
    public partial class PageControl
    {
        public PageControl()
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();
            _HomeItem.Name = "HomeItem";
            _ForwardItem.Name = "ForwardItem";
            _AfterItem.Name = "AfterItem";
            _EndItem.Name = "EndItem";
            _JumpItem.Name = "JumpItem";
            _BarButtonItem11.Name = "BarButtonItem11";
            _BarButtonItem12.Name = "BarButtonItem12";
            _BarButtonItem13.Name = "BarButtonItem13";

            // 在 InitializeComponent() 调用之后添加任何初始化。
        }


        /// <summary>
        /// 获取或设置当前页号
        /// </summary>
        /// <returns></returns>
        [DefaultValue(0)]
        public int LoadPage
        {
            get => this.m_LoadPage;
            set
            {
                this.m_LoadPage = value;
                this.BarLargeButtonItem1.Caption = $"第{this.LoadPage + 1}/{Math.Max(this.TotalPages, 1)}页,共{this.TotalCount}条记录";
            }
        }

        private int m_LoadPage = 0;

        /// <summary>
        /// 获取或设置每页显示的记录数
        /// </summary>
        /// <returns></returns>
        [DefaultValue(20)]
        public int LoadTop
        {
            get => this.m_LoadTop;
            set
            {
                this.m_LoadTop = value;
                this.BarSubItem1.Caption = $"每页显示:{value}条";
            }
        }

        private int m_LoadTop = 20;

        /// <summary>
        /// 获取总页数
        /// </summary>
        /// <returns></returns>
        [Browsable(false)]
        private int TotalPages
        {
            get
            {
                double page = this.TotalCount / (double)this.LoadTop;
                return (int)Math.Round(Math.Ceiling(page));
            }
        }

        private int m_TotalCount = 0;
        /// <summary>
        /// 设置或获取总行数
        /// </summary>
        /// <returns></returns>
        [DefaultValue(0)]
        public int TotalCount
        {
            get => this.m_TotalCount;
            set
            {
                this.m_TotalCount = value;
                this.BarLargeButtonItem1.Caption = $"第{this.LoadPage + 1}/{Math.Max(this.TotalPages, 1)}页,共{value}条记录";
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void BarButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPage = 0;
            ItemClick?.Invoke(this, e);
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void BarButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPage = Math.Max(0, this.LoadPage - 1);
            ItemClick?.Invoke(this, e);
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void BarButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int page = Math.Max(Math.Min(this.TotalPages - 1, this.LoadPage + 1), 0);
            this.LoadPage = page;
            ItemClick?.Invoke(this, e);
        }

        /// <summary>
        /// 末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void BarButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPage = Math.Max(this.TotalPages - 1, 0);
            ItemClick?.Invoke(this, e);
        }

        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void BarButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int page = 1;
            if (InputValue.Show(ref page, "请输入页号", "请输入您想前往的页号:"))
            {
                int loadPage = page - 1;
                loadPage = Math.Max(0, loadPage);
                loadPage = Math.Min(this.TotalPages - 1, loadPage);
                this.LoadPage = loadPage;
                ItemClick?.Invoke(this, e);
            }
        }

        private void BarButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadTop = Convert.ToInt32(e.Item.Caption);
            ItemClick?.Invoke(this, e);
        }

        public event EventHandler ItemClick;
    }
}